/**
 * @file     licenseKeyFormatting.ts
 * @brief    [482. 密钥格式化](https://leetcode.cn/problems/license-key-formatting/description/)
 * @author   Zhu
 * @date     2023-02-15 22:04
 */

function licenseKeyFormatting(s: string, k: number): string {
    let res = '';
    let sep = k;
    for (let i = s.length - 1; i >= 0; i--) {
        if (s[i] === '-') continue;
        let joint = '';
        if (sep == k && res !== '') {
            joint = '-';
        }
        res = s[i].toUpperCase() + joint + res;
        sep--;
        if (sep === 0) {
            sep = k;
        }
    }

    return res;
}
